package pl.shenlon.ga.evaluation;

import java.util.ArrayList;
import java.util.List;

import pl.shenlon.ga.Population;
import pl.shenlon.poker.cards.Card;
import pl.shenlon.poker.cards.Hand;
import pl.shenlon.poker.game.GameState;
import pl.shenlon.poker.game.PokerPlayer;
import pl.shenlon.poker.game.TestHand;
import pl.shenlon.poker.game.gametypes.NoLimitPokerGame;
import pl.shenlon.poker.game.gametypes.PokerGame;

public abstract class PopulationEvaluator {

	protected int minScore;

	public static final List<TestHand> TEST_HANDS;

	private static final List<GameState> gameStates;

	static {
		TEST_HANDS = new ArrayList<TestHand>();
		gameStates = new ArrayList<GameState>();
	}

	public static synchronized void setTestHands(Iterable<TestHand> hands) {
		TEST_HANDS.clear();
		gameStates.clear();
		for (TestHand hand : hands) {
			TEST_HANDS.add(hand);
			Hand player = hand.getPlayerACards(), table = hand.getTableCards();
			gameStates.add(new GameState(GameState.getWinChanceString(player,
					table), GameState.getStrenghtenChanceString(player, table),
					"", false));
			table.show(0, 1, 2);
			gameStates.add(new GameState(GameState.getWinChanceString(player,
					table), GameState.getStrenghtenChanceString(player, table),
					"", false));
			table.show(3);
			gameStates.add(new GameState(GameState.getWinChanceString(player,
					table), GameState.getStrenghtenChanceString(player, table),
					"", false));
			table.show(4);
			gameStates.add(new GameState(GameState.getWinChanceString(player,
					table), GameState.getStrenghtenChanceString(player, table),
					"", false));
			for (Card card : table.getCards()) {
				card.setFaceUp(false);
			}
		}

	}
	protected Class<? extends PokerGame> pokerGameClass = NoLimitPokerGame.class;

	public abstract PokerPlayer evaluatePopulation(TestHand hand);

	public Class<? extends PokerGame> getPokerGameClass() {
		return pokerGameClass;
	}

	public void setPokerGameClass(Class<? extends PokerGame> pokerGameClass) {
		this.pokerGameClass = pokerGameClass;
	}

	public abstract void setPopulation(Population<?> population);

	public static boolean testHandsContainsGameState(String winChance,
			String strenghtenChance) {
		for (GameState gameState : gameStates) {
			if (gameState.getWinChanceTerm().equals(winChance)
					&& gameState.getStrengthenChanceTerm().equals(
							strenghtenChance)) {
				return true;
			}
		}
		return false;
	}

}